<?php

require_once 'actions/manage_rejection.cls.php';
require_once 'actions/send_message.cls.php';

function echo_match($user, $match) {
	
	$block_url = r(new ManageRejectionAction())->get_direct_link();
	$hover_id = 'hover_' . $match->user_id;
	
	echo "<div class='match' onmouseover='$(\"#$hover_id\").show()' onmouseout='$(\"#$hover_id\").hide()'>";
		echo "<div class='profile-pic'>";
			if ($match->get_profile_img_url()) {
				echo "<img src='{$match->get_profile_img_url()}'>";
			}
		echo '</div>';
		echo '<div class="match-data">';
			echo "<div>";
				echo "<span class='name'>";
					echo $match->first_name . ' ' . $match->last_name . ' (' . $match->calculate_age() . ')';
				echo "</span>";
				if ($match->facebook) {
					echo "<a href='{$match->facebook}' target='_blank'><img src='static/img/facebook.png' class='icon'></a>";
				} 
				echo "<span id='$hover_id' class='options'>";
				if ($match->about_yourself) {
					$toggle_id = 'toggle_' . $match->user_id;
					$about_id = 'about_' . $match->user_id;
					echo "	<a href='#' id='$toggle_id'>more</a>
							<script type='text/javascript'>
								$('#$toggle_id').click(function() {
									if ($('#$toggle_id').text() == 'more') {
										$('#$about_id').slideDown();
										$('#$toggle_id').text('less');
									}
									else {
										$('#$about_id').slideUp();
										$('#$toggle_id').text('more');
									} 
									return false;
								});
							</script>";
				}
				if ($match != $user && !$user->invisible) {
					
					$block_url->set_value(ManageRejectionAction::INPUT_REJECTED_USER_ID, $match->user_id);
					
					if (in_array($match->user_id, $user->rejections)) {
						
						$block_url->set_value(ManageRejectionAction::INPUT_OPERATION, ManageRejectionAction::UNREJECT);
						$text = 'unblock';
						echo "<a href='{$block_url->__toString()}'>$text</a>";
						
					}
					else {
						
						$block_url->set_value(ManageRejectionAction::INPUT_OPERATION, ManageRejectionAction::REJECT);
						$text = 'block'; // is in fact reject: admins block users, users reject users.
					
						$overlay_id = "send-message_{$match->user_id}";
						echo "	<a href='javascript:create_send_message_overlay(\"$overlay_id\", \"Send a message to {$match->first_name}\");'>send message</a>
								<a href='{$block_url->__toString()}'>$text</a>";
						
						echo "<div id='$overlay_id' class='send-message'>
								<p>
									This message will be sent as an email, but your email address will not be visible. 
								</p>";
							$message_form = r(new SendMessageAction())->get_form_link();
							echo $message_form->start();
							echo $message_form->generate(new HiddenField(SendMessageAction::INPUT_DESTINATION, $match->user_id));
							echo $message_form->generate(new TextAreaField(SendMessageAction::INPUT_MESSAGE));
							echo $message_form->end();
						echo "
								<p>
									Privacy: we do not store the message itself, but we do keep records
									of who sent messages to who, for spam control. 
								</p>
							</div>";
						
					}
					
				}
				echo "</span>";
			echo '</div>';
			if ($match->about_yourself) {
				echo "<div id='$about_id' class='about' style='display:none;'>";
					echo 'About myself: ' . $match->about_yourself;
				echo '</div>';
			}
			echo '<div>';
				echo 'Speaks ' . $match->get_known_languages_str() .
				 	 ' and wants to learn ' . $match->get_unknown_languages_str();
			echo '</div>';
		echo '</div>';
		echo "<div style='clear:both;'></div>";
	echo '</div>';
	
}